package com.yc.boot.work.service.Impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.boot.sys.business.SysUtilBusiness;
import com.yc.boot.sys.model.UserInfo;
import com.yc.boot.work.entity.Test;
import com.yc.boot.work.mapper.TestMapper;
import com.yc.boot.work.model.find.TestPage;
import com.yc.boot.work.model.save.SaveTest;
import com.yc.boot.work.model.update.UpdateTest;
import com.yc.boot.work.service.TestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author:Yst
 * @Date:2021/11/10
 * @Time:13:38
 * @Info:
 */

@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    @Resource
    SysUtilBusiness sysUtilBusiness;

    @Override
    public boolean saveWithoutId(SaveTest param) {
        String now = DateUtil.now();
        UserInfo loginUser = sysUtilBusiness.getLoginUser();  //BaseEntity id,createBy,createTime,updateBy,updateTime,isDelete
        Test test = Convert.convert(Test.class, param);    //name,status
        test.setCreateTime(now);
        test.setCreateBy(loginUser.getId());
        return save(test);
    }

    @Override
    public boolean updateById(UpdateTest param) {
        String now = DateUtil.now();
        UserInfo loginUser = sysUtilBusiness.getLoginUser();
        Test test = Convert.convert(Test.class, param);
        test.setUpdateTime(now);
        test.setUpdateBy(loginUser.getId());
        return updateById(test);
    }

    @Override
    public IPage<Test> queryByPage(TestPage param) {
        IPage<Test> testIPage = this.page(
                new Page<>(param.getCurrent(), param.getSize()),


                new QueryWrapper<Test>().lambda()
                        .eq(
                                ObjectUtil.isNotNull(param.getId()),
                                Test::getId,
                                param.getId()
                        )
                        .like(
                                ObjectUtil.isNotNull(param.getName()),
                                Test::getName,
                                param.getName()
                        )
                        .le(
                                ObjectUtil.isNotNull(param.getCreateTimeEnd()),
                                Test::getCreateTime,
                                param.getCreateTimeEnd()
                        )
                        .ge(
                                ObjectUtil.isNotNull(param.getCreateTimeStart()),
                                Test::getCreateTime,
                                param.getCreateTimeStart()
                        )
        );
        return testIPage;
    }
}
